﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace TcpDownServer
{
    class UserManager
    {
        private Users Data;
        private string Basedir = string.Empty;

        private static UserManager mng = new UserManager();
        private UserManager()
        {
            Data = new Users();
            Basedir = AppDomain.CurrentDomain.BaseDirectory + "\\config.xml";
        }

        public static UserManager GetInstance()
        {
            return mng;
        }
        public void Init()
        {
            if (!File.Exists(Basedir))
            {
                Users.UserItemRow row = Data.UserItem.NewUserItemRow();
                row.Key = "sampleKey";
                row.QQ = "sampleQQ";
                Data.UserItem.AddUserItemRow(row);
                Data.WriteXml(Basedir, System.Data.XmlWriteMode.WriteSchema);
            }
            else
            {
                Data.ReadXml(Basedir, System.Data.XmlReadMode.ReadSchema);
            }
        }
        public bool CheckQQ(string QQ)
        {
            var rs = from row in Data.UserItem.AsEnumerable<Users.UserItemRow>()
                     where row.QQ == QQ
                     select row;
            Users.UserItemRow[] ls = rs.ToArray<Users.UserItemRow>();
            if (ls != null && ls.Length > 0)
            {
                return true;
            }
            return false;
        }

        public string GetKey(string QQ)
        {
            var rs = from row in Data.UserItem.AsEnumerable<Users.UserItemRow>()
                     where row.QQ == QQ
                     select row;
            Users.UserItemRow[] ls = rs.ToArray<Users.UserItemRow>();
            if (ls != null && ls.Length > 0)
            {
                return ls[0].Key;
            }
            return "";
        }


    }
}
